package com.scitc.pleasurevideo.woview;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.adapter.CachingGridviewAdapter;
import com.scitc.pleasurevideo.adapter.CollectGridviewAdapter;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.CollectTvDataMan;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.data.HistoryTvDataMan;
import com.scitc.pleasurevideo.dialog.DialogSettingRadio;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.pojo.CachingTvVo;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.pojo.MenuTvVo;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.util.OnClickUtil;
import com.scitc.pleasurevideo.view.SwDialog;

import java.util.ArrayList;
import java.util.List;

import static com.scitc.pleasurevideo.Tabhome.chingeAppbarLayout;

public class CollectActivity extends ETActivity {
    private ConstraintLayout nullHistoryItem;
    private GridView mGridView;
    private CollectGridviewAdapter madapter;
    private List<CachingTvVo> items = new ArrayList<>();

    private RelativeLayout rlHistoryGridview;
    private TextView numHistory;
    private ImageView igClearHistory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        initView();
        onClick();
        loadCollectTv();

    }

    /**
     * 加载收藏数据
     */
    private void loadCollectTv() {

        List<CachingTvVo> cachingTvVos = CollectTvDataMan.getCollectTvDataMan().getCachingTvVos();

        if (cachingTvVos == null || cachingTvVos.size() < 1) {
            setNullHistoryItemVisity();
            return;
        } else {
            setNullHistoryItemGone();
        }

        madapter.addAll(cachingTvVos);
        numHistory.setText("共" + cachingTvVos.size() + "个记录");
    }

    /**
     * 列表的点击事件
     * @param cachingTvVo
     */
    private void mgridViewClick(CachingTvVo cachingTvVo) {
        OnClickUtil.setViewClickTimes(mGridView);
        DoubanTvVo doubanTvPojo = new DoubanTvVo();
        doubanTvPojo.setTitle(cachingTvVo.getName());
        doubanTvPojo.setCover(cachingTvVo.getImgUrl());

        MenuTvVo menuTvPojo = new MenuTvVo();
        menuTvPojo.setUrl(cachingTvVo.getUrl());
        menuTvPojo.setTitle(cachingTvVo.getHtmlTitle());
        menuTvPojo.setPlayerString(cachingTvVo.getPalyListUrl());

        ForwardHelper.toTvPlayMenuActivity(mInstance, doubanTvPojo, menuTvPojo, cachingTvVo.getPlayMenuTvPojos());
    }


    /**
     * 显示对话框，是否全部删除收藏记录
     */
    private void showItemLongClick(){
        SwDialog exitDialog = new SwDialog(mInstance, "全部删除", "你确定要删除全部收藏记录吗？");
        exitDialog.setOkListener(() -> {
            CollectTvDataMan.getCollectTvDataMan().deleteAll();
            madapter.clearItem();
            setNullHistoryItemVisity();
        });
        exitDialog.show();
    }

    /**
     * 显示是否删除某一个收藏记录
     * @param news
     */
    private void showSwDialog(final CachingTvVo news) {
        SwDialog exitDialog = new SwDialog(mInstance, "删除操作", "你确定要删除 《" + news.getName() + "》 吗？");
        exitDialog.setOkListener(() -> {
            madapter.deleteItem(news.getId());
            CollectTvDataMan.getCollectTvDataMan().deleteCachingTvVo(news.getId());
            checkItemNum();
        });
        exitDialog.show();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        nullHistoryItem = (ConstraintLayout) findViewById(R.id.nullHistoryItem);
        mGridView = (GridView) findViewById(R.id.xGridView);
        rlHistoryGridview = (RelativeLayout) findViewById(R.id.rlHistoryGridview);
        numHistory = (TextView) findViewById(R.id.numHistory);
        igClearHistory = (ImageView) findViewById(R.id.igClearHistory);
        madapter = new CollectGridviewAdapter(mInstance, items);
        mGridView.setAdapter(madapter);
    }

    /**
     * 点击事件
     */
    private void onClick() {
        //列表的点击事件
        mGridView.setOnItemClickListener((adapterView, view, i, l) -> {
            CachingTvVo cachingTvVo = madapter.getItem(i);
            mgridViewClick(cachingTvVo);
        });
        //列表的长按事件
        mGridView.setOnItemLongClickListener((adapterView, view, i, l) -> {
            CachingTvVo news = madapter.getItem(i);
            showSwDialog(news);
            return true;
        });
        //清楚收藏
        igClearHistory.setOnClickListener(view -> showItemLongClick());

    }

    /**
     * 进入这个界面时
     * 加载收藏以及初始化状态栏是白底黑字
     */
    @Override
    public void onResume() {
        super.onResume();
        loadCollectTv();
        chingeAppbarLayout.OnChingeAppbarLayout(1);
    }

    /**
     * 检查列表还没有数据
     */
    private void checkItemNum() {
        if (madapter.getCount() == 0) {
            setNullHistoryItemVisity();
        }else {
            numHistory.setText("共" + madapter.getCount() + "个记录");
        }
    }

    /**
     * 设置列表没有数据时，显示提醒用户没有收藏记录
     */
    private void setNullHistoryItemVisity() {
        nullHistoryItem.setVisibility(View.VISIBLE);
        rlHistoryGridview.setVisibility(View.GONE);
    }

    /**
     * 设置列表有数据时，显示列表
     */
    private void setNullHistoryItemGone() {
        nullHistoryItem.setVisibility(View.GONE);
        rlHistoryGridview.setVisibility(View.VISIBLE);
    }


}
